using System.Collections.Generic;
using UnityEngine;

public class ArtifactManager : MonoBehaviour,ISaveArtifact
{
    public static ArtifactManager instance;
    private void Awake()
    {
        instance=this;
    }

    [Header("神器")]
    public List<ArtifactDatas> data = new List<ArtifactDatas>();
    [Header("是否拥有")]
    public List<bool> haveItem = new List<bool>();
    [Header("当前装备的神器")]
    public string currArtifact = "";
    private void Start()
    {
        //加载神器数据
        SaveManager.instance.SeekArtifactDataInterface();
        SaveManager.instance.GetArtifactInterface();
        SaveManager.instance.LoadArtifactData(Application.persistentDataPath, "Data/ArtifactData");
    }
    //更新数据
    public void UpdateData()
    {
        for(int i=0;i<haveItem.Count;i++)
        {
            data[i].isHave=haveItem[i];
        }
    }
    //添加神器
    public void AddArtifact(string str)
    {
        if(haveItem.Count<=0)
        {
            foreach(var i in data)
            {
                haveItem.Add(false);
            }
        }
        for(int i=0;i<data.Count;i++)
        {
            if (str == data[i].myName)
            {
                data[i].isHave=true;
                haveItem[i] = true;

                //神器成就
                AchiManager.instance.UnLockAchi("神器");

                TipSayManager.Instance.Say("获取神器 * <color=yellow> "+str+" </color>");
                break;
            }
        }
    }
    //查看是否佩戴神器
    public bool SeekArtifact(string str)
    {
        //除了拥有之外，还要装备
        for(int i=0;i< data.Count;i++)
        {
            if(data[i].myName==str&&haveItem[i]&&currArtifact==str)
            {
                return true;
            }
        }

        return false;
    }
    //查看是否拥有神器
    public bool SeekHave(string str)
    {
        //除了拥有之外，还要装备
        foreach (var i in data)
        {
            if (i.myName==str&&i.isHave)
            {
                return true;
            }
        }

        return false;
    }
    //装备神器
    public void Equipment(string str)
    {
        currArtifact=str;
    }
    //获取神器数据
    public ArtifactDatas GetArtifact(string str)
    {
        foreach(var i in data)
        {
            if(i.myName==str)
            {
                return i;
            }
        }

        return null;
    }
    public void Load(ArtifactData data)
    {
        if(data!=null)
        {
            haveItem=data.haveItem;
            currArtifact=data.currArtifact;
            TurnManager.instance.haveItem=data.haveItemTurn;
            TurnManager.instance.currOne=data.currOne;
            TurnManager.instance.currTow=data.currTow;
            GiftCode.instance.isGetList=data.giftCode;
            //赋值数据
            for (int i=0;i<haveItem.Count;i++)
            {
                this.data[i].isHave = haveItem[i];
            }
            //更新数据
            UpdateData();
        }
        else
        {
            haveItem.Clear();
            TurnManager.instance.haveItem.Clear();
            GiftCode.instance.isGetList.Clear();
            for (int i=0;i<this.data.Count;i++)
            {
                haveItem.Add(false);
            }
            for (int i = 0; i < haveItem.Count; i++)
            {
                this.data[i].isHave = false;

            }
            for (int i = 0; i < TurnManager.instance.datas.Count; i++)
            {
                TurnManager.instance.haveItem.Add(false);
            }
            for (int i = 0; i<GiftCode.instance.codes.Count; i++)
            {
                GiftCode.instance.isGetList.Add(false);
            }
            currArtifact = "";
            TurnManager.instance.currOne = "";
            TurnManager.instance.currTow = "";
        }
    }
    //清空
    public void Clear()
    {
        haveItem.Clear();
        TurnManager.instance.haveItem.Clear();
        GiftCode.instance.isGetList.Clear();
        for (int i = 0; i <data.Count; i++)
        {
            haveItem.Add(false);
            data[i].isHave = false;
            
        }
        currArtifact = "";
        for (int i = 0; i < TurnManager.instance.datas.Count; i++)
        {
            TurnManager.instance.haveItem.Add(false);
        }
        for(int i=0;i<GiftCode.instance.codes.Count;i++)
        {
            GiftCode.instance.isGetList.Add(false);
        }
        TurnManager.instance.currOne = "";
        TurnManager.instance.currTow = "";
    }
    public void Save(ref ArtifactData data)
    {
        if (data == null)
        {
            data = new ArtifactData();
        }

        data.haveItem=haveItem;
        data.haveItem = haveItem;
        data.currArtifact = currArtifact;
        data.haveItemTurn = TurnManager.instance.haveItem;
        data.currOne = TurnManager.instance.currOne;
        data.currTow = TurnManager.instance.currTow;
        data.giftCode=GiftCode.instance.isGetList;
    }
}
[System.Serializable]
public class ArtifactDatas
{
    [Header("名称")]
    public string myName;
    [Header("拥有")]
    public bool isHave = false;
    [Header("描述")]
    public string des;
    [Header("图片")]
    public Sprite Icon;
}